/*
 * =====================================================================================
 *    Description:  check a num's invertory
 *        Version:  1.0
 *        Created:  09/16/2012 06:25:29 PM
 * =====================================================================================
 */
#include	<stdio.h>
#include	<string.h>

char nums[20][132] = {{0x00}};
int  numsLen[20];

#define	MAX_ITERATER_TIMES 15			/*  */

bool isSelfInventory(const char *str, int len, char *invertoryStr, int & inventlen)
{
    int numCount[12] = {0x00};
    memset(numCount, 0x00, sizeof(numCount));
    for (int i = 0; i < len; ++i)
    {
        ++numCount[str[i]-'0'];
    }
    int idx = 0;
    for (int k = 0; k < 10; ++k)
    {
        if (numCount[k] > 9) {
            invertoryStr[idx++] = numCount[k] / 10 + '0';
            invertoryStr[idx++] = numCount[k] % 10 + '0';
            invertoryStr[idx++] = k + '0';
        } else if (numCount[k] > 0) {
            invertoryStr[idx++] = '0' + numCount[k];
            invertoryStr[idx++] = '0' + k;
        }
    }
    invertoryStr[idx] = '\0';
    inventlen = idx;
    if (idx != len)
        return false;
    if (strcmp(str, invertoryStr) == 0)
        return true;
    return false;
}

int check(int & jfrom, int &kstep)
{
    int i,j;
    int res = 0; // 1 is jth, 2 is loop
    for (i = 1; i <= MAX_ITERATER_TIMES; ++i)
    {
        if (isSelfInventory(nums[i-1], numsLen[i-1], nums[i], numsLen[i])) {
            res = 1;
            break;
        }
        for (j = 0; j < i-1; ++j) {
            if (numsLen[i] == numsLen[j] && strcmp(nums[i], nums[j]) == 0) {
                res = 2;
                break;
            }
        }
        if (res != 0) break;
    }
    //attention i's value
    if (res == 1) jfrom = i-1;
    else if (res == 2)
    {
        jfrom = j;
        kstep = i - j;
    }
    return res;
}

int main(int argc, char *argv[])
{
    char origNum[100];
    while (scanf("%s", origNum) == 1 && origNum[0] != '-')
    {
        int len = strlen(origNum);
        strcpy(nums[0], origNum);
        numsLen[0] = len;
        int j, k;
        int res = check(j, k);
        if (res == 1) {
            printf ( "%s is self-inventorying", origNum);
            if (j != 0)
                printf ( " after %d steps", j);
            printf ( "\n" );
        } else if (res == 2) {
            printf ( "%s enters an inventory loop of length %d\n", 
                    origNum, k);
        } else {
            printf ( "%s can not be classified after 15 iterations\n", origNum);
        }
    }
    return 0;
}
